# -*- coding: UTF-8 -*-
# Spider for 'asos.com'
#Author: Denis <sinedone@gmail.com>,

from urlparse import urljoin

from scrapy.spider import BaseSpider
from scrapy.http import  Request
from scrapy.selector import HtmlXPathSelector
from littlecrawler.items import ScrapedItem

class Netaporter(BaseSpider):

    name = 'netaporter'
    start_urls = ['http://www.net-a-porter.com/Shop/Clothing/All',
                  'http://www.net-a-porter.com/Shop/Shoes/All',
                  'http://www.net-a-porter.com/Shop/Bags/All',
                  'http://www.net-a-porter.com/Shop/Accessories/All',
                  'http://www.net-a-porter.com/Shop/Clothing/Lingerie',]
    URL = 'http://www.net-a-porter.com'
    visited = set()
    
    def parse(self, response):
        page = HtmlXPathSelector(response)
        
        for url in page.select("//div[@id='product-list']//a/@href").extract():
            if url not in self.visited:
                self.visited.add(url)
                yield Request(self.URL+url, callback=self.extract_data, encoding = 'latin-1')
                
        next_page = page.select("//div[@id='product-list-menu-bottom']//div[@class='page-nav']//a[contains(text(), 'Next')]/@href").extract()
        if next_page:
            yield Request(urljoin(response.url, next_page[0]), callback=self.parse, encoding = 'latin-1')

            
    def extract_data(self, response):
        page = HtmlXPathSelector(response)
        sex = 'woman'
        name = page.select('//div[@id="product-details"]//h2//text()').extract()[0].strip()
        price =  page.select('//div[@id="price"]//text()').extract()[0].strip()
        image1 =  page.select('//img[@id="medium-image"]//@src').extract()
        description =  page.select('//div[@class="productDescription"]//span[@class="en-desc"]/text()').extract()
        designer =  page.select('//div[@id="product-details"]//h1//a//text()').extract()
        type =  page.select('//div[@id="links-list"]//ul//li[2]//text()').extract()

        
        item = ScrapedItem()
        item['source'] = 'netaporter'
        item['sex'] = sex;
        item['name'] = name;
        item['price'] = price;
        item['image1'] = image1;
        item['image2'] = None;
        item['image3'] = None;
        item['image4'] = None;
        item['description'] = description;
        item['designer'] = designer;
        item['category1'] = None;
        item['type'] = type;
        item['facebookURL'] = response.url;
        item['url'] = response.url;
        yield item
        
        